package ift605.EquationWithTrainingAgents.behavior.special.complex;

import jade.core.AID;
import jade.lang.acl.ACLMessage;

import java.util.List;

import javax.naming.directory.InvalidAttributesException;

import ift605.EquationWithTrainingAgents.behavior.common.DeriverBehavior;
import ift605.EquationWithTrainingAgents.struct.DerivationInfo;
import ift605.EquationWithTrainingAgents.struct.DerivationStep;
import ift605.EquationWithTrainingAgents.struct.LearningMessage;
import udes.ds.agent.AbstractEquation;
import udes.ds.agent.MultiplicativeEquation;
import udes.ds.agent.SummativeEquation;

public class DecreaseNumericPartEquationBehavior extends DeriverBehavior {
	private static final long serialVersionUID = 1L;

	@Override
	public boolean derive(List<DerivationInfo> aDerivationInfos) {
		DerivationInfo current = aDerivationInfos.get(0);
		
		AbstractEquation base = current.Mess.base;
		AbstractEquation currentDerivation = current.Mess.derived;
		
		Object[] currentDerivationValues = currentDerivation.getValues();
		
		if(current.Mess.tryDerive)
		{
			aDerivationInfos.clear();
			
			for(int j =0; j < currentDerivationValues.length; ++j)
			{
				Integer variation = new Integer(j);
				DerivationInfo newInfo = new DerivationInfo((LearningMessage)Clone(current.Mess), variation);
				
				boolean firstArgumentIsNumeric =  Double.class.isAssignableFrom(currentDerivationValues[j].getClass()) || Integer.class.isAssignableFrom(currentDerivationValues[j].getClass());
				
				if(firstArgumentIsNumeric)
				{	
					AbstractEquation newDerivation = (AbstractEquation)Clone(newInfo.Mess.derived);
					Object[] newValues = newDerivation.getValues() ;
					

					if(newValues[j] instanceof Double)
					{
						newValues[j] =(Double)newValues[j] -1;
					}
					else //There we suppose an INT
					{
						newValues[j] =(Integer)newValues[j] -1 ;
					}
					
					try {
						newDerivation.setValues(newValues);
						
						newInfo.Mess.derived = newDerivation;
						
						aDerivationInfos.add(newInfo);
					
					} catch (InvalidAttributesException e) {
						e.printStackTrace();
					}
				}
			/*
				System.out.println(">> Decrease :: FROM");
				base.printUserReadable();
				currentDerivation.printUserReadable();
				System.out.println(">> TO -- " + j);
				newInfo.Mess.base.printUserReadable();
				newInfo.Mess.derived.printUserReadable();
				System.out.println("<<<<<<<<<<<<<<<<");*/
			}
		}
		else
		{
			Integer variation = (Integer) current.Variation;

			current.Mess.derived = (AbstractEquation) Clone(current.Mess.derived);
			Object[] newValues = current.Mess.derived.getValues();
			
			if(newValues[variation] instanceof Double)
			{
				newValues[variation] =(Double)newValues[variation] -1;
			}
			else //There we suppose an INT
			{
				newValues[variation] =(Integer)newValues[variation] -1 ;
			}
			
			try
			{
				current.Mess.derived.setValues(newValues);
			} 
			catch (InvalidAttributesException e) {
				e.printStackTrace();
			} 
		}
		return true;
	}
}